// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
const tencentcloud = require("tencentcloud-sdk-nodejs");
//文件中间件
const multer = require("multer")
// const uuid = require('uuid')
const fs = require('fs')
const cors = require('cors');
//阿里云存储中间件 配置模块
const Mao = require('multer-aliyun-oss');
// 阿里云存储
const OSS = require('ali-oss');
const express =  require('express')

const app = express();
const port =2000;
app.use(cors({
  //测试用*
  origin:"*",
  credentials:true
}));
//阿里云存储配置
const aliyunOss = {
  region:'oss-cn-beijing',//区域
  accessKeyId:'LTAI4FcfFwJTW6zhXX3o3UGS',
  accessKeySecret:'pEJoRvdJiTIHLFp8zUaBcd31RWdHb6',
  bucket:'xegs'//远程文件夹
}

const clientOss = new OSS(aliyunOss)
// 删除阿里云oss的文件
// 文件
async function deleteFile(file){
  try{
    let result = await clientOss.delete(file)
    // console.log(result,'返回值')
    return result
  }catch(e){
    console.log(e,'错误')
  }
};
//查询文件
async function getFile(file){
  try{
    let res = await clientOss.get(file);
    // console.log(res,'get返回')
    return res
  }catch(e){
    console.log(e,'getFileerr')
    return undefined
  }
}
//拷贝文件
async function copyFile(newFilePath,oldFilePath){
  try{
    let res = await clientOss.copy(newFilePath,oldFilePath)
    // console.log(res,'拷贝')
      return res
  }catch(e){
      console.log(e,'copyFileerr')
      return undefined
  }
}
//   文件路径    例如↓
// deleteFile('黄1515.jpg')
const custom = multer.diskStorage({
    //这个是本地服务器存储
    // destination(req,file,cb){
    //     cb(null,"./files")
    // },  
    async filename(req,file,cb){
      //说明是重新认证
      let extension = file.originalname.substr(file.originalname.lastIndexOf(".")).toLowerCase()
      if(req.body.reset==1){
        // 把之前文件拷贝一份
        let newFilePath='reset'+req.body.studentName+req.body.studentId+'.jpg',
        oldFilePath=req.body.studentName+req.body.studentId+'.jpg'
        const res = await copyFile(newFilePath,oldFilePath);
        // console.log(res,'存储里面res')
      }
      //获取文件的后缀名 并改成小写
      //生成唯一的名称
      let filename = req.body.studentName+req.body.studentId + extension;
      //新的文件名放进回调函数
      cb(null,filename)
    }
})

const oss = Mao({
  config:aliyunOss
})
// console.log(oss)
// console.log(custom,'custom')
const storage = Object.assign(oss,custom)
// console.log(storage,'aaa')
//使用自定义存储规则
const uploadPoint = multer({storage})

const IaiClient = tencentcloud.iai.v20200303.Client;

const clientConfig = {
  credential: {
    secretId: "AKIDX2WwHWegeZQG7yiMwMbnqphQ0OaZMxPP",
    secretKey: "11yqdGi8VmtsmV2MUOG2d9buBLaffYPF", 
  },
  region: "ap-chongqing",
  profile: {
    httpProfile: {
      endpoint: "iai.tencentcloudapi.com",
    },
  },
};

const client = new IaiClient(clientConfig);
// const params = {
//     // "Url":  "http://mfr8f9.natappfree.cc/b2839310-fa7f-11eb-8fc3-7d29fdedf466.jpg"
//     "Url":img
//     // Image:base64
// };
//DetectLiveFaceAccurate
//DetectLiveFace
//人脸比对 CompareFace
// 人脸验证 VerifyFace
//五官定位 AnalyzeFace
// client.DetectLiveFaceAccurate(params).then(
//   (data) => {

//     console.log(data,'data')
//   },
//   (err) => {
  
//     console.error("error", err);
//   }

// )

// iai.tencentcloudapi.com 
//  Action  DetectLiveFace
// Version  2020-03-03
// Region     ap-chongqing
// Image      base64编码
//




// app.use(express.static('./files'))
app.listen(port,()=>{
  console.log(`监听端口${port}`)
})